package CouponService

import (
	"QingDai-Gin/api/models"
	"QingDai-Gin/config"
	"gorm.io/gorm"
)

func GetAllCoupon() ([]models.Coupon, *gorm.DB) {
	var coupons []models.Coupon
	Mysql := config.Mysql()
	tx := Mysql.Table("commodity__coupon").Find(&coupons)
	return coupons, tx
}

func GetCouponById(id int) (models.Coupon, *gorm.DB) {
	var coupon models.Coupon
	Mysql := config.Mysql()
	tx := Mysql.Table("commodity__coupon").First(&coupon, id)
	return coupon, tx
}

func GetCouponsByP(p map[string]any) ([]models.Coupon, *gorm.DB) {
	var coupons []models.Coupon
	Mysql := config.Mysql()
	tx := Mysql.Table("commodity__coupon")
	for key, value := range p {
		tx.Where(key+" = ?", value)
	}
	tx.Find(&coupons)
	return coupons, tx
}
func GetCouponsByL(p map[string]any) ([]models.Coupon, *gorm.DB) {
	var coupons []models.Coupon
	Mysql := config.Mysql()
	tx := Mysql.Table("commodity__coupon")
	for key, value := range p {
		tx.Where(key+" like ?", value)
	}
	tx.Find(&coupons)
	return coupons, tx
}
func GetCouponByP(p map[string]any) (models.Coupon, *gorm.DB) {
	var coupon models.Coupon
	Mysql := config.Mysql()
	tx := Mysql.Table("commodity__coupon")
	for key, value := range p {
		tx.Where(key+" = ?", value)
	}
	tx.First(&coupon)
	return coupon, tx
}

func CreateCoupon(coupon models.Coupon) {
	Mysql := config.Mysql()
	Mysql.Table("commodity__coupon").Create(coupon)
}

func UpdateCoupon(coupon models.Coupon) {
	Mysql := config.Mysql()
	Mysql.Table("commodity__coupon").Save(coupon)
}

func DeleteCoupon(coupon models.Coupon) {
	Mysql := config.Mysql()
	Mysql.Table("commodity__coupon").Delete(coupon)
}
